package com.example.constellation;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.widget.RadioGroup;

import com.example.constellation.bean.StarBean;
import com.example.constellation.luckyfrag.LuckyFragment;
import com.example.constellation.mefrag.MeFragment;
import com.example.constellation.partnerfrag.PartnerFragment;
import com.example.constellation.starfrag.StarFragment;
import com.example.constellation.utils.AssetsUtils;
import com.google.gson.Gson;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener
{
    RadioGroup mainRg;
    //声明两个按钮对应的Fragment对象
    Fragment starFrag,luckFrag,partnerFrag,meFrag;
    private FragmentManager manager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mainRg=findViewById(R.id.main_rg);

        //设置监听点击了哪个单选按钮
        mainRg.setOnCheckedChangeListener(this);
//加载星座相关数据 /assets/xzcontent/xzcontent.json
        StarBean infoBean=loadData();
        Bundle bundle=new Bundle();
        bundle.putSerializable("info",infoBean);  //bundle里面放置对象  key是bundle对象的键值





        //创建碎片对象
        starFrag=new StarFragment();
        starFrag.setArguments(bundle);
        partnerFrag=new PartnerFragment();
        partnerFrag.setArguments(bundle);
        meFrag=new MeFragment();
        meFrag.setArguments(bundle);
        luckFrag=new LuckyFragment();
        luckFrag.setArguments(bundle);
        //将四个Fragment进行动态加载 一起加载到动态布局当中1.replace 加载较快 2.add/hide/show
        addFragmentPage();

    }
    /*
    读取assets文件夹下的xzcontent.json文件

     */
    private StarBean loadData()
    {
        String json= AssetsUtils.getJsonFromAssets(this,"xzcontent/xzcontent.json");
        Gson gson=new Gson();
        StarBean starInfoBean = gson.fromJson(json, StarBean.class);
        AssetsUtils.saveBitmapFromAssets(this,starInfoBean);
        return starInfoBean;
    }




    //将主页当中的碎片一起加载进入布局 有用的显示 暂时无用的隐藏
    private void addFragmentPage() {
        //1.创建碎片管理者对象
        manager = getSupportFragmentManager();
        //2.创建处理事务的对象
        FragmentTransaction transaction=manager.beginTransaction();
        //3.将四个Fragment统一添加到布局当中
        transaction.add(R.id.main_layout_center,starFrag);
        transaction.add(R.id.main_layout_center,partnerFrag);
        transaction.add(R.id.main_layout_center,meFrag);
        transaction.add(R.id.main_layout_center,luckFrag);
        //4.隐藏后面的三个
        transaction.hide(partnerFrag);
        transaction.hide(meFrag);
        transaction.hide(luckFrag);

        transaction.commit();


    }


    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        FragmentTransaction transaction = manager.beginTransaction();


        switch(i)
        {
            case R.id.main_rb_star:
                transaction.show(starFrag);
                transaction.hide(partnerFrag);
                transaction.hide(meFrag);
                transaction.hide(luckFrag);



                break;
            case R.id.main_rb_luck:
                transaction.show(luckFrag);
                transaction.hide(partnerFrag);
                transaction.hide(meFrag);
                transaction.hide(starFrag);

                break;
            case R.id.main_rb_me:
                transaction.show(meFrag);
                transaction.hide(partnerFrag);
                transaction.hide(starFrag);
                transaction.hide(luckFrag);

                break;
            case R.id.main_rb_partner:
                transaction.show(partnerFrag);
                transaction.hide(starFrag);
                transaction.hide(meFrag);
                transaction.hide(luckFrag);

                break;
        }
        transaction.commit();

    }
}